package com.ktjiaoyu.crm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ktjiaoyu.crm.pojo.User;
import com.ktjiaoyu.crm.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

/**
 * @ClassName : LoginController
 * @Description :
 * @Author : Lzz
 * @Date: 2023/11/22 08:45:48
 */
@Controller
public class LoginController {

    @Resource
    UserService userService;

    @RequestMapping("/toLogin")
    public String toLogin(){

        return "login";
    }

    @RequestMapping(value = "/login")
    public String login(String usrName, String usrPassword, Model model, HttpSession session){
        User user = userService.findUserByUsrName(usrName);
        if(user == null){
            model.addAttribute("msg","用户不存在，登录失败！");
            return "forward:/toLogin";
        }
        if(!user.getUsrPassword().equals(usrPassword)){
            model.addAttribute("msg","密码不正确，登录失败！");
            return "forward:/toLogin";
        }
        session.setAttribute("loginUser",user);
        return "main";
    }

    @RequestMapping(value = "/logout")
    public String logout(HttpSession session){
        session.removeAttribute("loginUser");   //删除session中保存的用户信息
        return "login";
    }

}
